package stockService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;


@WebServlet(urlPatterns = {"/data"})
public class dataService extends HttpServlet {
    private static final long serialVersionUID = 1L;

    static String data = "";

    public static void setData(int cur) {
        if (cur != -1) {
            loadData.cur = cur;
        }
        data = "";
        loadData stock = loadData.stocks[loadData.cur];
        data = data + stock.code;//0
        data = data + ",";
        data = data + stock.name;//1
        data = data + ",";
        data = data + stock.today;//2
        data = data + ",";
        data = data + stock.last;//3
        data = data + ",";
        data = data + stock.current;//4
        data = data + ",";
        data = data + stock.highest;//5
        data = data + ",";
        data = data + stock.lowest;//6
        data = data + ",";
        data = data + stock.quan;//7
        data = data + ",";
        data = data + stock.price;//8
        data = data + ",";
        for (int i = 0; i < 5; i++) {
            data = data + stock.bquan[i];//9-13
            data = data + ",";
        }
        for (int i = 0; i < 5; i++) {
            data = data + stock.bprice[i];//14-18
            data = data + ",";
        }
        for (int i = 0; i < 5; i++) {
            data = data + stock.squan[i];//19-23
            data = data + ",";
        }
        for (int i = 0; i < 5; i++) {
            data = data + stock.sprice[i];//24-28
            data = data + ",";
        }
        data = data + new DecimalFormat("#.##%").format(stock.perc);//29
        data = data + ",";
        if(stock.perc < 0){
            data = data + "-";//30
        }
        else{
            data = data + "+";
        }
        data = data + ",";
    }

    public String getData() {
        return data;
    }


    public dataService() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html; charset=utf-8");
        PrintWriter out = response.getWriter();
        try {
            response.setHeader("Cache-Control", "no-store");
            response.setHeader("Pragma", "no-cache");
            response.setDateHeader("Expires", 0);
            String stock = request.getParameter("stock");
            setData(Integer.parseInt(stock));
            out = response.getWriter();
            out.println(getData());
        } catch (Exception e) {
            out.print("error: data");
            e.printStackTrace();
        }

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}